<route>
  {
    name: "login",
    meta: {
      "public": true,
    }
  }
</route>

<template>
  <div class="login" @keydown.enter="handleSubmit">
    <div class="login-con">
      <Card :bordered="false">
        <p slot="title">
          <Icon type="log-in"></Icon> {{ $t('login.title') }}
        </p>
        <div class="form-con">
          <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem prop="account">
              <Input v-model="form.account" :placeholder="$t('login.form.account')">
              <span slot="prepend">
                <Icon :size="15" type="md-person"></Icon>
              </span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password" v-model="form.password" :placeholder="$t('login.form.password')">
              <span slot="prepend">
                <Icon :size="16" type="md-lock"></Icon>
              </span>
              </Input>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" long>{{ $t('login.form.button') }}</Button>
            </FormItem>
          </Form>
          <div class="lang">
            <RadioGroup v-model="lang" @on-change="onLangChanged">
              <Radio v-for="(val,label) in langs" :key="val" :label="label"></Radio>
            </RadioGroup>
          </div>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        langs: {
          '简体中文': 'zh',
          'English': 'en',
        },
        lang: '简体中文',
        form: {
          account: 'superadmin',
          password: '',
        },
        rules: {
          account: [
            { required: true, message: this.$t('login.validate.account.need'), trigger: 'blur' }
          ],
          password: [
            { required: true, message: this.$t('login.validate.password.need'), trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.$storage.delUser()
      this.lang = this.$storage.storeGet('lang') || '简体中文'
    },
    methods: {
      onLangChanged() {
        this.$i18n.locale = this.langs[this.lang]
      },
      handleSubmit() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.$storage.storeSet('lang', this.lang)
            this.$md.dispatch('login', this.form).then(data => {
              if (data.retCode === 1) {
                this.$storage.setUser({
                  account: data.account,
                  nick: data.nick,
                  email: data.email,
                  inited: data.inited,
                  roles: data.roles,
                  token: data.token,
                  version: data.version,
                })
                // 超级管理员跳转
                this.$router.push({ name: 'dash' })
              } else {
                switch (data.retMsg) {
                  case 'server.notfound':
                    this.$Message.error(this.$t(data.retMsg, [this.$t('login.form.account')]))
                    break
                  case 'server.mismatch':
                    this.$Message.error(this.$t(data.retMsg, [this.$t('login.form.password')]))
                    break
                  default:
                    this.$Message.error(this.$t(data.retMsg))
                    break
                }
              }
            })
          }
        })
      }
    }
  };
</script>

<style lang="less" scoped>
  .login {
    width: 100%;
    height: 100%;
    background-image: url('/static/images/login-bg.jpg');
    background-size: cover;
    background-position: center;
    position: relative;
    &-con {
      position: absolute;
      right: 160px;
      top: 50%;
      transform: translateY(-60%);
      width: 300px;
      &-header {
        font-size: 16px;
        font-weight: 300;
        text-align: center;
        padding: 30px 0;
      }
      .form-con {
        padding: 10px 0 0;
      }
      .login-tip {
        font-size: 10px;
        text-align: center;
        color: #c3c3c3;
      }
      .lang {
        text-align: center;
      }
    }
  }
</style>
